package com.blog.security;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.blog.pojo.Member;
import com.blog.pojo.Role;

/** 
 * @author ant 
 * @date 创建时间：2017年8月16日 下午4:47:03  
 */
public class SecurityUser extends Member implements UserDetails {

	private static final long serialVersionUID = 4951158383802814033L;

	public SecurityUser(Member member) {
		if (member != null) {
			this.setMemberId(member.getMemberId());
			this.setMemberAddress(member.getMemberAddress());
			this.setMemberAge(member.getMemberAge());
			this.setMemberAvatar(member.getMemberAvatar());
			this.setMemberEmail(member.getMemberEmail());
			this.setMemberPhone(member.getMemberPhone());
			this.setMemberName(member.getMemberName());
			this.setMemberPassword(member.getMemberPassword());
			this.setMemberQq(member.getMemberQq());
			this.setMemberSex(member.getMemberSex());
			this.setMemberWechat(member.getMemberWechat());
			this.setRoleList(member.getRoleList());
			this.setMemberStatus(member.getMemberStatus());
		}
	}
	
	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		Collection<GrantedAuthority> authorities = new ArrayList<>();  
        List<Role> userRoles = this.getRoleList();            
        if(userRoles != null)  {  
            for (Role role : userRoles) {  
                SimpleGrantedAuthority authority = new SimpleGrantedAuthority(role.getRoleName());  
                authorities.add(authority);  
            }  
        }  
        return authorities;  
	}

	@Override
	public String getPassword() {
		return super.getMemberPassword();
	}
	
	@Override
	public String getUsername() {
		return super.getMemberName();
	}
	
	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}

}
